//
//  ASXViewController.swift
//  PassValueDemo
//
//  Created by MAC on 2020/8/25.
//  Copyright © 2020 MAC. All rights reserved.
//

import UIKit

class ASXViewController: UIViewController {
    //属性传值 - string
    var myStr = String()
    //属性传值 - array
    var arr:[String] = []
    //属性传值 - 全局引用实例方法 - 1
    var counter = Counter()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //返回上级界面的按钮
        let ymBtn_01 = UIButton()
        ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
        ymBtn_01.setTitle("返回上个页面", for: .normal)
        ymBtn_01.backgroundColor = .brown
        ymBtn_01.tag = 10001
        ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
        view.addSubview(ymBtn_01)
        
        //接收传值的标签
        let textLabel = UILabel()
        textLabel.font = .systemFont(ofSize: 20)
        textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: view.frame.size.width - 100, height: 50)
        textLabel.text = myStr
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.backgroundColor = .darkGray
        textLabel.textColor = .white
        view.addSubview(textLabel)
    }
    
    // 视图将要显示时调用该方法
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("视图即将显示")
        /*
         由于viewDidLoad()无法接收传值，所以只能在视图即将显示的时候，来接收上级界面所传的值
         */
        //打印接收的数组
        for item in self.arr {
            //打印接收数组的值
            print(item)
        }
    }
    
    //实现按钮的监听方法
    @objc func tiaozhuanyemianclick(sender:UIButton){
        //开始返回上级界面
        self.navigationController?.popViewController(animated: true)
    }
}
// MARK: -  全局引用实例方法 - 2
/*
 通过实例方法也可以做到属性传值
 */
class Counter {
    func increment(amount:Int) {
        print("===amount:",amount)
    }
}
